/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.FrameControlKhoHang;

import GUI.FrameControlDanhMuc.jframeThemSach;
import TOOL.Ulti;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import GUI.AutoSuggest;
import GUI.jframeMain;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 10520_000
 */
public final class jframePhieuNhapSach extends javax.swing.JFrame {

//    private javax.swing.JComboBox jComboBoxMaSach1;
//    private javax.swing.JComboBox jComboBoxTenSach1;
    
    /**
     * Creates new form jframePhieuNhapSach
     */
    public jframePhieuNhapSach() throws SQLException {
        
        ResultSet res = new BUS.Sach().GetAll();
        try {
            _tenSach = new AutoSuggest(res, 2, 260, 22, 154, 170);
            this.add(_tenSach);
        } catch (SQLException ex) {
            Logger.getLogger(jframeMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        _tenSach.getComboBox().addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _tenSach.getComboBox().actionPerformed(evt);
                LoadInfo();
            }
        });
        
        initComponents();
        
        jTextFieldSoLuongNhap.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((c >= '0') && (c <= '9') ||
                    (c == KeyEvent.VK_BACK_SPACE) ||
                    (c == KeyEvent.VK_DELETE))) {
                getToolkit().beep();
                e.consume();
          }
        }
        });
        
        jTextFieldDonGiaNhap.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((c >= '0') && (c <= '9') ||
                    (c == KeyEvent.VK_BACK_SPACE) ||
                    (c == KeyEvent.VK_DELETE))) {
                getToolkit().beep();
                e.consume();
          }
        }
        });
        
//        jComboBoxMaSach1 = new JComboBox();
//        jComboBoxTenSach1 = new JComboBox();
//        jTableSach.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jComboBoxMaSach1));
//        jTableSach.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jComboBoxTenSach1));
        jTextFieldMaPhieuNhap.setText(new BUS.PhieuNhapSach().GenerateID());
        Initialize();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabelTieuDe = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldMaPhieuNhap = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jDateChooserNgayLap = new com.toedter.calendar.JDateChooser();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldTacGia = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldTheLoai = new javax.swing.JTextField();
        jTextFieldTenSach = new javax.swing.JTextField();
        jButton1ThemSach1 = new javax.swing.JButton();
        jTextFieldMaSach = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldDonGiaNhap = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldSoLuongNhap = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextFieldSoLuongTon = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jButton1ThemSach = new javax.swing.JButton();
        jButtonThemMoi = new javax.swing.JButton();
        jButtonSua = new javax.swing.JButton();
        jButtonXoa = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSach = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldTongCong = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jButtonLuuThoat = new javax.swing.JButton();
        jButtonThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lập phiếu nhập sách");

        jPanel2.setBackground(new java.awt.Color(49, 122, 233));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabelTieuDe.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabelTieuDe.setForeground(new java.awt.Color(255, 255, 255));
        jLabelTieuDe.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        jLabelTieuDe.setText("Lập phiếu nhập sách");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTieuDe)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTieuDe)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Mã phiếu nhập:");

        jTextFieldMaPhieuNhap.setEnabled(false);

        jLabel6.setText("Ngày lập:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldMaPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(100, 100, 100))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jDateChooserNgayLap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextFieldMaPhieuNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(51, 51, 51));
        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel2.setText("Mã sách:");

        jLabel3.setText("Tên sách:");

        jLabel4.setText("Tác giả:");

        jTextFieldTacGia.setEnabled(false);

        jLabel5.setText("Thể loại:");

        jTextFieldTheLoai.setEnabled(false);

        jTextFieldTenSach.setEditable(false);
        jTextFieldTenSach.setEnabled(false);

        jButton1ThemSach1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add1.png"))); // NOI18N
        jButton1ThemSach1.setText("Cập nhật Sách");
        jButton1ThemSach1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ThemSach1ActionPerformed(evt);
            }
        });

        jTextFieldMaSach.setEditable(false);
        jTextFieldMaSach.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldTacGia, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                    .addComponent(jTextFieldTheLoai)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jTextFieldMaSach)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1ThemSach1))
                    .addComponent(jTextFieldTenSach))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jButton1ThemSach1)
                    .addComponent(jTextFieldMaSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldTenSach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldTacGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(51, 51, 51));
        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel7.setText("Đơn giá nhập:");

        jLabel8.setText("Số lượng nhập:");

        jLabel9.setText("Số lượng tồn:");

        jTextFieldSoLuongTon.setEnabled(false);

        jLabel12.setText("(VNĐ)");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldSoLuongTon, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                    .addComponent(jTextFieldSoLuongNhap)
                    .addComponent(jTextFieldDonGiaNhap))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addContainerGap(65, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextFieldSoLuongTon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextFieldSoLuongNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldDonGiaNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBackground(new java.awt.Color(51, 51, 51));
        jPanel5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButton1ThemSach.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add1.png"))); // NOI18N
        jButton1ThemSach.setText("Thêm sách");
        jButton1ThemSach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ThemSachActionPerformed(evt);
            }
        });

        jButtonThemMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/new.png"))); // NOI18N
        jButtonThemMoi.setText("Thêm sách mới");
        jButtonThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThemMoiActionPerformed(evt);
            }
        });

        jButtonSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/sua.png"))); // NOI18N
        jButtonSua.setText("Sửa chi tiết nhập");
        jButtonSua.setEnabled(false);
        jButtonSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSuaActionPerformed(evt);
            }
        });

        jButtonXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/del.png"))); // NOI18N
        jButtonXoa.setText("Xóa chi tiết nhập");
        jButtonXoa.setEnabled(false);
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jButton1ThemSach)
                .addGap(52, 52, 52)
                .addComponent(jButtonThemMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(jButtonSua)
                .addGap(51, 51, 51)
                .addComponent(jButtonXoa)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1ThemSach)
                    .addComponent(jButtonThemMoi)
                    .addComponent(jButtonXoa)
                    .addComponent(jButtonSua))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTableSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Mã sách", "Tên sách", "Số lượng nhập", "Đơn giá nhập", "Thành tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableSach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSach);

        jPanel6.setBackground(new java.awt.Color(51, 51, 51));
        jPanel6.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel10.setText("Tổng cộng:");

        jTextFieldTongCong.setEditable(false);
        jTextFieldTongCong.setText("000");
        jTextFieldTongCong.setEnabled(false);

        jLabel11.setText("(VNĐ)");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(60, Short.MAX_VALUE)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTongCong, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTextFieldTongCong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel7.setBackground(new java.awt.Color(51, 51, 51));
        jPanel7.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButtonLuuThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/save1.png"))); // NOI18N
        jButtonLuuThoat.setText("Lưu Phiếu");
        jButtonLuuThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLuuThoatActionPerformed(evt);
            }
        });

        jButtonThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close2.png"))); // NOI18N
        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(jButtonLuuThoat)
                .addGap(109, 109, 109)
                .addComponent(jButtonThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(89, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonLuuThoat)
                    .addComponent(jButtonThoat))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSuaActionPerformed
        // TODO add your handling code here:
        if(jButtonSua.getText().contains("Sửa"))
        {
            _tenSach.getComboBox().setSelectedItem(((Object)((DefaultTableModel)jTableSach.getModel()).getValueAt(jTableSach.getSelectedRow(), 2).toString()));
            LoadInfo();
            jTextFieldSoLuongNhap.setText(((DefaultTableModel)jTableSach.getModel()).getValueAt(jTableSach.getSelectedRow(), 3).toString());
            jTextFieldDonGiaNhap.setText(((DefaultTableModel)jTableSach.getModel()).getValueAt(jTableSach.getSelectedRow(), 4).toString());
            jButtonSua.setText("Lưu chi tiết nhập");
        }
        else
        {
            ((DefaultTableModel)jTableSach.getModel()).setValueAt(jTextFieldMaSach.getText(), jTableSach.getSelectedRow(), 1);
            ((DefaultTableModel)jTableSach.getModel()).setValueAt(_tenSach.getComboBox().getSelectedItem(), jTableSach.getSelectedRow(), 2);
            ((DefaultTableModel)jTableSach.getModel()).setValueAt(jTextFieldSoLuongNhap.getText(), jTableSach.getSelectedRow(), 3);
            ((DefaultTableModel)jTableSach.getModel()).setValueAt(jTextFieldDonGiaNhap.getText(), jTableSach.getSelectedRow(), 4);
            ((DefaultTableModel)jTableSach.getModel()).setValueAt((Integer.parseInt(jTextFieldSoLuongNhap.getText()) * Integer.parseInt(jTextFieldDonGiaNhap.getText())), jTableSach.getSelectedRow(), 5);
            
            int thanhtien = Integer.parseInt(jTextFieldDonGiaNhap.getText()) * Integer.parseInt(jTextFieldSoLuongNhap.getText());
            int tong = thanhtien + Integer.parseInt(jTextFieldTongCong.getText());
            jTextFieldTongCong.setText("" + tong);
            jButtonSua.setText("Sửa chi tiết nhập");
        }
            
    }//GEN-LAST:event_jButtonSuaActionPerformed

    private void jButtonThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThemMoiActionPerformed
        // TODO add your handling code here:
       new jframeThemSach(null).show();
    }//GEN-LAST:event_jButtonThemMoiActionPerformed

    private void jButton1ThemSachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ThemSachActionPerformed
        // TODO add your handling code here:
        //them vao table
        int thanhtien = Integer.parseInt(jTextFieldDonGiaNhap.getText()) * Integer.parseInt(jTextFieldSoLuongNhap.getText());
        int tong = thanhtien + Integer.parseInt(jTextFieldTongCong.getText());
        jTextFieldTongCong.setText("" + tong);
        ((DefaultTableModel)jTableSach.getModel()).addRow(new Object[]
        {
            jTableSach.getRowCount()+ 1,jTextFieldMaSach.getText(), _tenSach.GetSelectItem(),
            jTextFieldSoLuongNhap.getText(), jTextFieldDonGiaNhap.getText(), thanhtien
                
        });
    }//GEN-LAST:event_jButton1ThemSachActionPerformed

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        // TODO add your handling code here:
          for(int i = jTableSach.getSelectedRow(); i < jTableSach.getRowCount(); i++)
            ((DefaultTableModel)jTableSach.getModel()).setValueAt(i, i, 0);
        ((DefaultTableModel)jTableSach.getModel()).removeRow(jTableSach.getSelectedRow());
        jButtonSua.setEnabled(false);
        jButtonXoa.setEnabled(false);
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jButtonLuuThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLuuThoatActionPerformed
        // TODO add your handling code here:
        
        if(Insert())
        {
            Object[] options = {"Có", "Không"};
            int n = JOptionPane.showOptionDialog(this, "Lưu phiếu thành công. Lập phiếu mới?", "Thông báo",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            
            if(n != 0) dispose();
            else
            {
               ((DefaultTableModel)jTableSach.getModel()).setNumRows(0);
                try {
                    Initialize();
                } catch (SQLException ex) {
                    Logger.getLogger(jframePhieuNhapSach.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }//GEN-LAST:event_jButtonLuuThoatActionPerformed

    private void jTableSachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSachMouseClicked
        // TODO add your handling code here:
        if(jTableSach.getSelectedRow() != -1)
        {
            jButtonSua.setEnabled(true);
            jButtonXoa.setEnabled(true);
        }
    }//GEN-LAST:event_jTableSachMouseClicked

    private void jButton1ThemSach1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ThemSach1ActionPerformed
        try {
            // TODO add your handling code here:
            Initialize();
        } catch (SQLException ex) {
            Logger.getLogger(jframePhieuNhapSach.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ThemSach1ActionPerformed
    
    /*
     * Lay va do thong tin cua sach dc conj vao cac o tuong ung
     */
    private void LoadInfo()
    {
        try 
        {
            ResultSet res = new BUS.Sach().GetInfo(_tenSach.GetSelectItem().toString().trim());
            jDateChooserNgayLap.setDate(Calendar.getInstance().getTime());
            if(res.next())
            {
                jTextFieldMaSach.setText(res.getObject(1).toString());
                jTextFieldTheLoai.setText(res.getObject(2).toString());
                jTextFieldTacGia.setText(res.getObject(3).toString());
                jTextFieldSoLuongTon.setText(res.getObject(4).toString());
            }
        } 
        catch (SQLException ex) 
        {
            System.err.println("Loi gi do");
        }
    }
    
    /*
     *return Chen phieu nhap vao CSDL
     */
     
    private boolean Insert()
    {
        if(jTableSach.getRowCount() < 1)
        {
            Object[] options = {"Nhập lại", "Thoát"};
            int n = JOptionPane.showOptionDialog(this, "Chi tiết phiếu không được trống", "Lỗi",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.ERROR_MESSAGE, null, options, options[0]);
            if(n == 0)
                return false;
            else
            {
                this.dispose();
                return false;
            }
        }
        new DTO.PhieuNhapSach(Ulti.S(jTextFieldMaPhieuNhap),
                jDateChooserNgayLap.getDate().toString()).Insert();
        //Lap chi tiet hoa don
        for(int i = 0; i < jTableSach.getRowCount(); i++)
        {
            new DTO.ChiTietPhieuNhap(new BUS.ChiTietPhieuNhap().GenerateID(),
                    Ulti.S(jTextFieldMaPhieuNhap),((DefaultTableModel)jTableSach.getModel()).getValueAt(i, 1).toString(),
                    ((DefaultTableModel)jTableSach.getModel()).getValueAt(i, 3).toString(), ((DefaultTableModel)jTableSach.getModel()).getValueAt(i, 4).toString()).Insert();
        }
        return true;
    }
   //Khoi tao cac gia tri can thiet
    public void Initialize() throws SQLException
    {
        
        
        LoadInfo();
        jDateChooserNgayLap.setDate(Calendar.getInstance().getTime());
        jTextFieldSoLuongNhap.setText("000");
        jTextFieldSoLuongNhap.selectAll();
        jTextFieldDonGiaNhap.setText("000");
        jTextFieldDonGiaNhap.selectAll();
    }
    
    AutoSuggest _tenSach;
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1ThemSach;
    private javax.swing.JButton jButton1ThemSach1;
    private javax.swing.JButton jButtonLuuThoat;
    private javax.swing.JButton jButtonSua;
    private javax.swing.JButton jButtonThemMoi;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JButton jButtonXoa;
    private com.toedter.calendar.JDateChooser jDateChooserNgayLap;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelTieuDe;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableSach;
    private javax.swing.JTextField jTextFieldDonGiaNhap;
    private javax.swing.JTextField jTextFieldMaPhieuNhap;
    private javax.swing.JTextField jTextFieldMaSach;
    private javax.swing.JTextField jTextFieldSoLuongNhap;
    private javax.swing.JTextField jTextFieldSoLuongTon;
    private javax.swing.JTextField jTextFieldTacGia;
    private javax.swing.JTextField jTextFieldTenSach;
    private javax.swing.JTextField jTextFieldTheLoai;
    private javax.swing.JTextField jTextFieldTongCong;
    // End of variables declaration//GEN-END:variables
}
